-- INCLUDES
require "luasql.odbc";

-- TODO: figure out how to get the local path
dofile("/usr/local/freeswitch/scripts/SEPROJECT/paths.lua");
dofile("/usr/local/freeswitch/scripts/SEPROJECT/function_in_se.lua");
--dofile("/usr/local/freeswitch/scripts/IIITD/function.lua");

script_name = "project37.lua";
digits = "";
arg = {};
GlobalCnt = 0;
start="N";
Ending = "N"
sessid = os.time();
userid = 'nil';
adminforums = {};
line_num = session:getVariable("destination_number");
aosd = basedir .. "/scripts/SEPROJECT/wavfiles";		
phonenum = session:getVariable("caller_id_number");
--phonenum = phonenum:sub(-10);
input="";
rep = "N";		
logfile:write(sessid, "\t",
		    session:getVariable("caller_id_number"), "\t", session:getVariable("destination_number"), "\t", 
		    os.time(), "\t", "Call Started", "\t", "\n");




------------------------------------------------------------------------------------------------------------------------
-------validate caller--------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

function authorize_caller()
 session:execute("set_audio_level", "write 1");
	while (session:ready() == true) do

		
   		query = "select * from table_studentdata where Col_mobileNo = " .. phonenum ;
   		freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
   		cur = assert (con:execute(query));
   		row = {};result = cur:fetch(row);
   		cur:close();

                
		if (tostring(row[3]) == 'nil') then
      			role = "unregistered_number";
                        freeswitch.consoleLog("info", "unregistered");
		end
		if (tostring(row[3]) == phonenum) then
      			role="registered_number";
		end			
		break;
	end
end

------------------------------------------------------------------------------------------------------------------------
-------Registered User Menu---------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------


function registered_number()

        session:answer();
        session:setAutoHangup(false);
        --freeswitch.consolelog("registered number");
        freeswitch.consoleLog("info", "registered" .. " : actual caller_id " .. phonenum .. "\n");
        --While loop will come
        while(session:ready()==true) do

              digits=session:read(4,4,aosd .. "/Pin.wav",3000,"#");
              freeswitch.consoleLog("info",digits);
              
                query = "select * from table_studentdata where Col_mobileNo = " .. phonenum ;
   	        freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
   		cur = assert (con:execute(query));
   		row = {};result = cur:fetch(row);
   		cur:close();
                local counter=0;
                local flag=0;
                if(digits==tostring(row[4]))then
                           freeswitch.consoleLog("info", "you are have succesfully authenticated yourself".. "\n");
                           --call the marks function
                           get_marks(tostring(row[5]))
                           break;
                end
                while(true) do
              
                     digits="";
                           digits=session:read(4,4,aosd .. "/Wrongpin.wav",3000,"#"); --tone for wrong pin enter again
                           freeswitch.consoleLog("info",digits);
                      
    
                      if(digits==tostring(row[4]))then
                           freeswitch.consoleLog("info", "you are have succesfully authenticated yourself".. "\n");
                           --call the marks function
                           get_marks(tostring(row[5]))
                           break;
                           
                      else
                           
                           counter=counter+1;
                      end
              
                     if(counter==2)then
                         flag=1;
                         break;
                      end           
               end
       
            if(flag==1) then
                 --play error message limits exceeded
                 session:read(0,0,aosd .. "/PinError.wav",3000,"#");
                 break;
            end
             break;
        end
       
end



------------------------------------------------------------------------------------------------------------------------
-------UnRegistered User Menu---------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

function unregistered_number()

        session:answer();
        session:setAutoHangup(false);
        freeswitch.consoleLog("info", "unregistered2");
        --file for unregistered number
        while(session:ready()==true) do
       digits=session:read(4,4,aosd .. "/Unregistered.wav",3000,"#");
       freeswitch.consoleLog("info",digits);
       local num=digits;
       
       query = "select * from table_studentdata where Col_mobileNo = " .. num ;
   		freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
   		cur = assert (con:execute(query));
   		row = {};result = cur:fetch(row);
   		cur:close();
        local flag2=0;
        local counter2=0;
        if (tostring(row[3]) == num) then
      			--session:read(1,1,aosd .. "/prompts/nextA.wav",500,"#");
                        digits="";
                        digits=session:read(4,4,aosd .. "/Pin.wav",3000,"#");
                        freeswitch.consoleLog("info",digits);
                        
                        query = "select * from table_studentdata where Col_mobileNo = " .. num ;
   	                freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
   		        cur = assert (con:execute(query));
   		        row = {};result = cur:fetch(row);
   		        cur:close();
                        local counter=0;
                        local flag=0;

                        if(digits==tostring(row[4]))then
                                freeswitch.consoleLog("info", "you are have succesfully authenticated yourself".. "\n");
                                --call the marks function
                                get_marks(tostring(row[5]))
                                break;
                        end

                        while(true) do
              
                           digits="";
                           digits=session:read(4,4,aosd .. "/Wrongpin.wav",3000,"#"); --tone for wrong pin enter again
                           freeswitch.consoleLog("info",digits);
                      
    
                           if(digits==tostring(row[4]))then
                                     freeswitch.consoleLog("info", "you are have succesfully authenticated yourself".. "\n");
                                     --call the marks function
                                      get_marks(tostring(row[5]))
                                     break;
                           
                           else
                           
                               counter=counter+1;
                           end
              
                           if(counter==2)then
                                flag=1;
                                break;
                           end           
                        end
       
                       if(flag==1) then
                          --play error message limits exceeded
                          session:read(0,0,aosd .. "/PinError.wav",3000,"#");
                          break;
                       end
        end
        while(true) do
              digits="";
              digits=session:read(4,4,aosd .. "/wrongUnregistered.wav",3000,"#");
              freeswitch.consoleLog("info",digits);
              local num=digits;
              query = "select * from table_studentdata where Col_mobileNo = " .. num ;
   	      freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
   	      cur = assert (con:execute(query));
   	      row = {};result = cur:fetch(row);
   	      cur:close();
               
              if (tostring(row[3]) == num) then
                           digits=session:read(4,4,aosd .. "/Pin.wav",3000,"#");
                        freeswitch.consoleLog("info",digits);
                        
                        query = "select * from table_studentdata where Col_mobileNo = " .. num ;
   	                freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
   		        cur = assert (con:execute(query));
   		        row = {};result = cur:fetch(row);
   		        cur:close();
                        local counter=0;
                        local flag=0
                        if(digits==tostring(row[4]))then
                                freeswitch.consoleLog("info", "you are have succesfully authenticated yourself".. "\n");
                                --call the marks function
                                get_marks(tostring(row[5]))
                                break;
                        end
                        while(true) do
              
                           digits="";
                           digits=session:read(4,4,aosd .. "/Wrongpin.wav",3000,"#"); --tone for wrong pin enter again
                           freeswitch.consoleLog("info",digits);
                      
    
                           if(digits==tostring(row[4]))then
                                     freeswitch.consoleLog("info", "you are have succesfully authenticated yourself".. "\n");
                                     --call the marks function
                                      get_marks(tostring(row[5]))
                           break;
                           
                           else
                           
                               counter=counter+1;
                           end
              
                           if(counter==2)then
                                flag=1;
                           break;
                           end           
                        end
       
                       if(flag==1) then
                          --play error message limits exceeded
                          session:read(0,0,aosd .. "/PinError.wav",3000,"#");
                          break;
                       end
                       break;
              else
                  counter2=counter2+1;
              end
              if(counter2==2)then
                  flag2=1;
                  break;
              end

        end
      if(flag2==1)then
           session:read(0,0,aosd .. "/ErrorUnregisteredMobile.wav",3000,"#");
           break;
      end  
      break;            
      end                 
end




function get_marks(rollno)

      digits=session:read(1,1,aosd .. "/option.wav",3000,"#");
      if(digits=="1")then
      query = "select * from table_studentmarks where Col_rollnumber = " .. rollno ;
   		freeswitch.consoleLog("info", script_name .. " : SQL Querry = " .. query .. "\n");
   		cur = assert (con:execute(query));
   		row = {};result = cur:fetch(row);
   		cur:close();
      session:read(0,0,aosd .. "/marks.wav",1000,"#");
      session:execute("set_audio_level", "write 4");
             session:say(tostring(row[2]),"en","number","iterated");
             session:say(tostring(row[2]),"en","number","pronounced");
             session:execute("set_audio_level", "write 0");
      end
end
----------------------------------------------------------------------------------------------------
------------START---START----START----START----START----START---------------------------------------
----------------------------------------------------------------------------------------------------

session:answer();
session:setVariable("playback_terminators", "#");
session:setHangupHook("hangup");
session:setInputCallback("my_cb", "arg");
--freeswitch.consoleLog("info", script_name .. " : debug " .. "\n");
authorize_caller();
freeswitch.consoleLog("info", script_name .. " : actual caller_id " .. phonenum .. "\n");
session:setVariable("phonenum", phonenum);
--generate a menu
session:set_tts_parms("flite", "rms");
while (session:ready() == true) do
	
	read(basedir .. "/scripts/IIITD/sounds/tune.wav",100);	

        read(aosd .."/welcome.wav",100);
	
	if (role == "registered_number") then
		registered_number();
	end
	if (role == "unregistered_number") then
		
		unregistered_number();
--			
	end
freeswitch.consoleLog("info", "did you reach here\n");
		session:read(1,1,aosd .."/bye.wav",200,"#");	
	break;
	
end
session:hangup();
